﻿namespace PillBox.Client.Controls
{
    using System.Windows;
    using System.Windows.Controls;

    public class ContentControl2 : ContentControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ContentControl2"/> class.
        /// </summary>
        public ContentControl2()
        {
            this.HorizontalContentAlignment = HorizontalAlignment.Stretch;
            this.VerticalContentAlignment = VerticalAlignment.Stretch;
        }

        #region TemplateSelector Property
        /// <summary>
        /// Identifies the TemplateSelector dependency property.
        /// </summary>
        public static readonly DependencyProperty TemplateSelectorProperty =
            DependencyProperty.Register(
                "TemplateSelector",
                typeof(ITemplateSelector),
                typeof(ContentControl2),
                new PropertyMetadata(null, OnTemplateSelectorChanged));

        /// <summary>
        /// Gets or sets the TemplateSelector.
        /// </summary>
        /// <value>The value of TemplateSelector.</value>
        public ITemplateSelector TemplateSelector
        {
            // IMPORTANT: To maintain parity between setting a property in
            // XAML and procedural code, do not touch the getter and setter
            // inside this dependency property!
            get
            {
                return (ITemplateSelector)GetValue(TemplateSelectorProperty);
            }

            set
            {
                SetValue(TemplateSelectorProperty, value);
            }
        }

        /// <summary>
        /// Called when TemplateSelector has changed.
        /// </summary>
        /// <param name="oldValue">The old value.</param>
        /// <param name="newValue">The new value.</param>
        protected virtual void OnTemplateSelectorChanged(ITemplateSelector oldValue, ITemplateSelector newValue)
        {
            UpdateTemplate(this.Content);
        }

        /// <summary>
        /// Called when TemplateSelector has changed.
        /// </summary>
        /// <param name="o">The target dependency object.</param>
        /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void OnTemplateSelectorChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            ContentControl2 contentControl2 = o as ContentControl2;
            if (contentControl2 != null)
            {
                contentControl2.OnTemplateSelectorChanged((ITemplateSelector)e.OldValue, (ITemplateSelector)e.NewValue);
            }
        }
        #endregion

        public void RefreshTemplate()
        {
            this.UpdateTemplate(this.Content);
        }

        protected override void OnContentChanged(object oldContent, object newContent)
        {
            base.OnContentChanged(oldContent, newContent);

            UpdateTemplate(newContent);
        }

        private void UpdateTemplate(object content)
        {
            if (this.TemplateSelector != null)
            {
                this.ContentTemplate = this.TemplateSelector.SelectTemplate(content, this);
            }
        }
    }
}